package com.mqttinsight.ui.form.panel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.mqttinsight.exception.VerificationException;
import com.mqttinsight.mqtt.SecureSetting;
import com.mqttinsight.util.LangUtil;
import com.mqttinsight.util.Validator;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;

/**
 * @author ptma
 */
@Slf4j
public class ServerOnlyPanel extends SecurePanel {

    private JPanel rootPanel;
    private JTextField serverCertificateFileField;
    private JLabel serverCertificateFileLabel;

    public ServerOnlyPanel() {
        super();
        $$$setupUI$$$();
        initSecureFileBrowserButton(serverCertificateFileField);
        applyLanguage();
    }

    @Override
    public void applyLanguage() {
        serverCertificateFileLabel.setText(LangUtil.getString("CaCertificateFile"));
    }

    @Override
    public JPanel getRootPanel() {
        return rootPanel;
    }

    @Override
    public void changeFieldsEnable(boolean enabled) {
        serverCertificateFileField.setEnabled(enabled);
    }

    @Override
    public void applySetting(SecureSetting setting) {
        if (setting != null) {
            serverCertificateFileField.setText(setting.getServerCertificateFile());
        }
    }

    @Override
    public void resetFields() {
        serverCertificateFileField.setText("");
    }

    @Override
    public void verifyFields() throws VerificationException {
        Validator.notEmpty(serverCertificateFileField, () -> LangUtil.format("FieldRequiredValidation", serverCertificateFileLabel.getText()));
    }

    @Override
    public SecureSetting getSetting() throws VerificationException {
        SecureSetting setting = new SecureSetting();
        setting.setServerCertificateFile(serverCertificateFileField.getText());
        return setting;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        rootPanel = new JPanel();
        rootPanel.setLayout(new FormLayout("fill:170px:noGrow,left:4dlu:noGrow,fill:d:grow", "center:d:noGrow"));
        serverCertificateFileLabel = new JLabel();
        serverCertificateFileLabel.setText("CA certificates file");
        CellConstraints cc = new CellConstraints();
        rootPanel.add(serverCertificateFileLabel, cc.xy(1, 1));
        serverCertificateFileField = new JTextField();
        rootPanel.add(serverCertificateFileField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }

}
